#include "undomanager.h"

UndoManager::UndoManager(QObject *parent)
	: QObject(parent)
{

}

UndoManager::~UndoManager()
{
	qDeleteAll(m_undoMap);
	m_undoMap.clear();
}

void UndoManager::addUndoableObject( QObject* pObject )
{
	Q_ASSERT(!m_undoMap.contains(pObject));
	UndoData* pUndoData = new UndoData;
	pUndoData->pUndoAct = pUndoData->undoStack.createUndoAction(pObject);
	pUndoData->pRedoAct = pUndoData->undoStack.createRedoAction(pObject);
	m_undoMap[pObject] = pUndoData;
}

void UndoManager::removeUndoableObject( QObject* pObject )
{
	Q_ASSERT(m_undoMap.contains(pObject));
	UndoData* pUndoData = m_undoMap[pObject];
	m_undoMap.remove(pObject);
	delete pUndoData;
}

QAction* UndoManager::currentUndoAction()
{
	Q_ASSERT(m_currentUndoObject);
	return m_undoMap[m_currentUndoObject]->pUndoAct;
}

QAction* UndoManager::currentRedoAction()
{
	Q_ASSERT(m_currentUndoObject);
	return m_undoMap[m_currentUndoObject]->pRedoAct;
}

QUndoStack& UndoManager::currentUndoStack()
{
	Q_ASSERT(m_currentUndoObject);
	return m_undoMap[m_currentUndoObject]->undoStack;
}

QPointer<QObject> UndoManager::currentUndoObject() const
{
	return m_currentUndoObject;
}

void UndoManager::setCurrentUndoObject( QObject* pObject )
{
	if(m_currentUndoObject != pObject)
	{
		m_currentUndoObject = pObject;
		emit currentUndoObjectChanged(pObject);
	}
}
